import { asyncRouterMap, constantRouterMap } from '@/router'
import user from './user'

/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
function hasPermission(roles, route=[]) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param routes asyncRouterMap
 * @param roles
 */
function filterAsyncRouter(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRouter(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}
/**
 *将后台的路由表进行格式化
 * @param {*} asyncRouterMap
 */
function convertRouter(asyncRouterMap) {
    const accessedRouters = []
    if (asyncRouterMap) {
        asyncRouterMap.forEach(item => {
            var parent = generateRouter(item)
            var children = []
            if (item.children) {
                item.children.forEach(child => {
                    children.push(generateRouter(child))
                })
            }
            parent.children = children
            accessedRouters.push(parent)
        })
    }
    accessedRouters.push({ path: '*', redirect: '/404', hidden: true })
    return accessedRouters
}
function generateRouter(item) {
    item.component = componentsMap[item.component]
    // var router = {
    //   path: item.path,
    //   name: item.name,
    //   meta: item.meta,
    //   // component: isParent ? Layout : () => import(item.component)
    //   component: componentsMap[item.name]
    // }
    return item
}
const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers=[]) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({commit }, data) {
      return new Promise(resolve => {
        const { serverRouterMap } = data
        let accessedRouters
        // if (roles.includes('admin')) {
        //   accessedRouters = asyncRouterMap
        // } else {
        //   accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
        // }
        //   console.log("asyncRouterMap",asyncRouterMap);
        //   console.log("serverRouterMap",serverRouterMap);
        //   console.log("user",user);
          asyncRouterMap.map((e,i)=>{
              if(e.name == 'Terminal'){
                  if(user.state.isChain == 0){   //不是连锁
                    if(e.redirect == '/terminalShopList'){
                        asyncRouterMap.splice(i,1);
                    }
                  }if(user.state.isChain == 1){    //是连锁
                      if(e.redirect == '/terminalShopDetail'){
                          asyncRouterMap.splice(i,1);
                      }
                  }
              }
          });


          accessedRouters = asyncRouterMap;
          if(serverRouterMap && serverRouterMap.length > 0)
              accessedRouters = convertRouter(serverRouterMap)
          // console.log("accessedRouters",accessedRouters)
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    }
  }
}

export default permission
